//
//  LYPlaybackHeadCell.m
//  LYLiveApp
//
//  Created by QD on 2017/7/18.
//  Copyright © 2017年 览益信息科技. All rights reserved.
//

#import "LYPlaybackHeadCell.h"
#import "LYPlaybackModel.h"

@interface LYPlaybackHeadCell ()
@property (weak, nonatomic) IBOutlet UILabel *teacherTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property(nonatomic,assign) CGFloat titleHeight;
@property(nonatomic,assign) CGFloat contentHeight;
@property (weak, nonatomic) IBOutlet UILabel *bottomLabel;
@property (weak, nonatomic) IBOutlet UIView *upContentView;
@property (weak, nonatomic) IBOutlet UIView *downContentView;
@property (weak, nonatomic) IBOutlet UIButton *upButton;
@property (weak, nonatomic) IBOutlet UIButton *downButton;

@end

@implementation LYPlaybackHeadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.teacherTitleLabel.layer.cornerRadius = 3.0f;
    self.teacherTitleLabel.layer.masksToBounds = YES;
   
    self.upContentView.layer.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
    self.upContentView.layer.shadowOffset = CGSizeMake(0,1);
    self.upContentView.layer.shadowOpacity = 0.8;
    self.upContentView.layer.shadowRadius = 1;
    
    self.downContentView.layer.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
    self.downContentView.layer.shadowOffset = CGSizeMake(0,1);
    self.downContentView.layer.shadowOpacity = 0.8;
    self.downContentView.layer.shadowRadius = 1;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCellAction)];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tapGesture];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

- (void)setModel:(LYPlaybackModel *)model {

    _model = model;
    self.teacherTitleLabel.text = model.name;
    self.nameLabel.text = [NSString stringWithFormat:@"主讲：%@",model.teacher];
    if (model.actTime.length) {
        self.timeLabel.text = [NSString stringWithFormat:@"直播时间：%@",model.actTime];
    } else {
        self.timeLabel.text = [NSString stringWithFormat:@"直播时间：--"];
    }
    self.titleLabel.text = [NSString stringWithFormat:@"%@",model.vtitle];
    self.contentLabel.text = [NSString stringWithFormat:@"内容：%@",model.vcontent];
    
    CGFloat titleHeight = [LYUtil sizeWithText:model.vtitle font:self.titleLabel.font withWidth:SCREEN_WIDTH - 20].height;
    self.titleLabel.height = titleHeight;
    self.titleHeight = titleHeight;
    
    self.teacherTitleLabel.origin = CGPointMake(10, self.titleLabel.origin.y + titleHeight + 10);
    self.nameLabel.origin = CGPointMake(90, self.titleLabel.origin.y + titleHeight + 10);
    self.upContentView.height = titleHeight + 55;
    self.downContentView.hidden = YES;
    
    CGFloat contentHeight =   [LYUtil sizeWithText:self.contentLabel.text font:self.contentLabel.font withWidth:SCREEN_WIDTH - 20].height;
    self.contentHeight = contentHeight;
    self.contentLabel.height = contentHeight;
    self.bottomLabel.origin = CGPointMake(10, self.contentLabel.origin.y + contentHeight + 10);
    self.downContentView.height = contentHeight + 100;
    
    if (self.model.isOpen) {
        self.downContentView.hidden = NO;
        self.downContentView.origin = CGPointMake(0, self.upContentView.height);
        self.height = titleHeight + contentHeight + 185;
        self.upButton.hidden = YES;
    } else {
        self.downContentView.hidden = YES;
        self.height = titleHeight + 80;
        self.upButton.hidden = NO;
    }
    
    self.bottomLabel.text = @"内容仅代表个人观点，与本平台立场无关\n不对您构成任何投资建议";

}

- (IBAction)expandAction:(id)sender {
    self.model.isOpen = YES;
    self.arrowBlock ? self.arrowBlock(self.titleHeight + self.contentHeight + 185):nil;

}

- (IBAction)closeAction:(id)sender {
    
    self.model.isOpen = NO;
    self.arrowBlock ? self.arrowBlock(self.titleHeight + 85):nil;

}

- (void)tapCellAction {

    if (self.model.isOpen) {
        [self closeAction:nil];
    } else {
        [self expandAction:nil];
    }

}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"LYPlaybackHeadCell";
    LYPlaybackHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"LYPlaybackHeadCell" owner:self options:nil] lastObject];
    }
    return cell;
}

@end
